<?php
class Administrator_AdvertisingController extends Zend_Controller_Action
{	
    public function init() {
       
    }
    public function indexAction()
    {
   
    	try {
           $model = new HT_Model_administrator_models_advertising();
           $dataList = $model->getAll("1 =1 order by id desc");
            $paginator         = Zend_Paginator::factory($dataList);
            $currentPage       = 1;
            //Check if the user is not on page 1
            $page              = $this->_getParam('page');
            if (! empty($page)) { //Where page is the current page
                $currentPage   = $this->_getParam('page');
            }
            
            //Set the properties for the pagination
            
            $paginator->setItemCountPerPage(50);
            $paginator->setPageRange(20);
            $paginator->setCurrentPageNumber($currentPage);
            Zend_Paginator::setDefaultScrollingStyle('Sliding');
            Zend_View_Helper_PaginationControl::setDefaultViewPartial('pagination.phtml');
            $paginator->setView($this->view);
            $this->view->data = $paginator;   
            $this->view->dataPosition = $model->listPosition();   
         }catch(Exception $ex){
            echo "<pre>";
            print_r($ex->getMessage());
        }
    
    }
    public function addAction() {
       
        try {
           $model = new HT_Model_administrator_models_advertising();
           $form                   = new Administrator_Form_Advertising();
           if ($this->_request->isPost()) {
                $formData = $this->_request->getPost();
                if ($form->isValid($formData)) {
                   $dataInsert = $form->getValues();
                   $dataInsert['time'] = strtotime($dataInsert['time']);
                   $model->insert($dataInsert);
                   $this->_redirect('/administrator/advertising');
                }
            }
           //set view
           $this->view->form       = $form;
        }catch(Exception $ex){
            echo "<pre>";
            print_r($ex->getMessage());
        }
    }
    public function editAction() {
        try {
           $model = new HT_Model_administrator_models_advertising();
           $form                   = new Administrator_Form_Advertising();
           $form->getElement('url')->setRequired(false);
           $id                     =  $this->_request->getParam('id',false);
           $data = $model->findById($id);
           $data['time'] = date('Y-m-d', $data['time']);
           $form->populate($data);
           if ($this->_request->isPost()) {
                $formData = $this->_request->getPost();
    
                if ($form->isValid($formData)) {
                   $dataInsert = $form->getValues();
                   $dataInsert['time'] = strtotime($dataInsert['time']);
                   if($dataInsert['url'] == NULL){
                    unset($dataInsert['url']);
                   }
                   $model->update($dataInsert, "id = $id");
                   $this->_redirect('/administrator/advertising');
                }
            }
           //set view
           $this->view->form       = $form;
        }catch(Exception $ex){
            echo "<pre>";
            print_r($ex->getMessage());
        }
    }
    
	public function deleteAction() {
     $id                     =  $this->_request->getParam('id',false);
     $model = new HT_Model_administrator_models_advertising();
     $where = "id = $id";
     $model->delete($where);
     $this->_redirect($_SERVER['HTTP_REFERER']);
  }
    	
}
